package com.lancoo.ccas52.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lancoo.ccas52.entity.*;
import com.lancoo.ccas52.mapper.CourseMapper;
import com.lancoo.ccas52.mapper.CourseWeekMapper;
import com.lancoo.ccas52.mapper.StudentMapper;
import com.lancoo.ccas52.mapper.SubClassMapper;
import com.lancoo.ccas52.pojo.common.PageInfo;
import com.lancoo.ccas52.pojo.vo.ClassDto;
import com.lancoo.ccas52.pojo.vo.SubCourseVo;
import com.lancoo.ccas52.service.SubClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lancoo.ccas52.service.TeachingClassService;
import com.lancoo.ccas52.util.PagingUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 拆班班级 服务实现类
 * </p>
 *
 * @author quin.song
 * @since 2023-10-30
 */
@Service
public class SubClassServiceImpl extends ServiceImpl<SubClassMapper, SubClass> implements SubClassService {

    @Resource
    private TeachingClassService teachingClassService;

    @Resource
    private CourseMapper courseMapper;
    @Resource
    private CourseWeekMapper courseWeekMapper;
    @Resource
    private StudentMapper studentMapper;


    @Override
    public List<SubClass> addSubClass(List<SubClass> subClasses) {
        this.saveOrUpdateBatch(subClasses);
        return subClasses;
    }

    @Override
    public boolean removeSubClass(List<Long> ids) {
        List<SubClass> subClassList = this.lambdaQuery().in(SubClass::getId, ids).list();
        if (CollUtil.isNotEmpty(subClassList)) {
            this.removeByIds(ids);
            List<TeachingClass> subTeachingClassList = teachingClassService.lambdaQuery().eq(TeachingClass::getTaskId, subClassList.get(0).getTaskId())
                    .in(TeachingClass::getUniqueShiftSign, subClassList.stream().map(SubClass::getUniqueShiftSign).collect(Collectors.toList())).list();
            teachingClassService.deleteClass(subTeachingClassList.stream().map(TeachingClass::getTeachingClassId).collect(Collectors.toList()));
        }
        return true;
    }

    @Override
    public PageInfo<SubClass> getClassSub(Integer taskId, Integer type, String gradeId, String keyword, Integer currentPage, Integer pageSize) {
        Page<SubClass> page = new Page<>(currentPage, pageSize);
        Page<SubClass> studentClassSub = this.lambdaQuery()
                .eq(SubClass::getTaskId, taskId)
                .eq(ObjUtil.isNotNull(type), SubClass::getType, type)
                .eq(StrUtil.isNotBlank(gradeId), SubClass::getGradeId, gradeId)
                .like(StrUtil.isNotBlank(keyword), SubClass::getClassName, keyword).page(page);
        return PagingUtil.formatPageResult(studentClassSub);

    }

    @Override
    public List<SubCourseVo> getSubCourse(Integer taskId, String collegeId, Integer majorProp, String classId) {
        List<SubCourseVo> subCourse = courseMapper.getSubCourse(taskId, collegeId, majorProp);
        //排除行政班已选课程
        if (StrUtil.isNotBlank(classId)) {
            List<String> baseCourseId = this.lambdaQuery().eq(SubClass::getTaskId, taskId).like(SubClass::getClassId, classId).list()
                    .stream().map(SubClass::getCourseId).flatMap(courseId -> Arrays.stream(courseId.split(","))).collect(Collectors.toList());
            subCourse.removeIf(a -> baseCourseId.contains(a.getBaseCourseId()));
        }
        if (CollUtil.isEmpty(subCourse)) {
            return subCourse;
        }

        List<Long> courseIds = subCourse.stream().map(SubCourseVo::getCourseId).collect(Collectors.toList());
        //填充设置得周次相关信息
        Map<String, List<CourseWeek>> weekMap = courseWeekMapper.selectAllByCourseId(courseIds)
                .stream().collect(Collectors.groupingBy(o -> o.getCourseId() + "-" + o.getStudyNature()));
        subCourse.forEach(a -> {
            List<CourseWeek> courseWeeks = weekMap.get(a.getCourseId() + "-" + a.getStudyNature());
            a.setTeachingClassWeeks(BeanUtil.copyToList(courseWeeks, TeachingClassWeek.class));
        });
        //排查未设置课程参数的的课程
        subCourse.removeIf(a -> CollUtil.isEmpty(a.getTeachingClassWeeks()));
        return subCourse;
    }

    @Override
    public List<ClassDto> getSubClass(Integer taskId, String keyWord, String baseCourseId) {
        List<ClassDto> classDto = studentMapper.getClassDto(taskId, null, null, null);
        if (StrUtil.isNotBlank(keyWord)) {
            classDto = classDto.stream().filter(a -> a.getClassName().contains(keyWord)).collect(Collectors.toList());
        }
        if (StrUtil.isNotBlank(baseCourseId)) {
            List<String> classIds = this.lambdaQuery().eq(SubClass::getTaskId, taskId).like(SubClass::getCourseId, baseCourseId).list()
                    .stream().map(SubClass::getClassId).flatMap(classId -> Arrays.stream(classId.split(","))).collect(Collectors.toList());
            classDto = classDto.stream().filter(a -> !classIds.contains(a.getClassId())).collect(Collectors.toList());
        }
        return classDto;
    }


}
